package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.core.domain.LExamSort;
import com.learn.core.mapper.LExamSortMapper;
import com.learn.core.service.ILExamSortService;
import com.learn.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LExamSortServiceImpl
 * @date 2021.03.22 11:05
 */
@Service
public class LExamSortServiceImpl extends BaseServiceImpl<LExamSortMapper, LExamSort> implements ILExamSortService {
    @Autowired
    LExamSortMapper lExamSortMapper;

    @Override
    public List<LExamSort> queryLExamSortAll(QueryWrapper<LExamSort> queryWrapper) {
        List<LExamSort> treeMenus =new ArrayList<>();
        List<LExamSort> lExamSortList = lExamSortMapper.selectList(queryWrapper);
        // 查询树结构
        for(LExamSort lExamSort : getRootNode(lExamSortList)) {
            lExamSort=buildChilTree(lExamSort,lExamSortList);
            treeMenus.add(lExamSort);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private LExamSort buildChilTree(LExamSort pNode,List<LExamSort> menuList){
        List<LExamSort> chilMenus =new ArrayList<>();
        for(LExamSort menuNode : menuList) {
            if(menuNode.getParentNode() == pNode.getExamSortId()) {
                chilMenus.add(buildChilTree(menuNode,menuList));
            }
        }
        pNode.setChilLExamSort(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<LExamSort> getRootNode(List<LExamSort> menuList) {
        List<LExamSort> rootMenuLists =new  ArrayList<LExamSort>();
        for(LExamSort menuNode : menuList) {
            if(menuNode.getParentNode() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
